package com.ceo.common.export;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TableHeader
{
	private static int defColumnWidth=1000;
	private static DataFormater df_formater=new DefaultFormater();//默认的数据处理类
	public String fieldName;//标题名称
	public String fieldKey;//标题绑定的数据源属性或者key
	public int columnWidth=defColumnWidth;//列宽
	private DataFormater formater;//数据格式化对象
	private List<TableHeader> childFiled;//包含的子列
	private TableHeader parentHeader;//父级列
	public TableHeader(String fieldName)
	{
		this.fieldName=fieldName;
		this.formater=df_formater;
	}
	public TableHeader(String fieldKey, String fieldName)
	{
		this.fieldKey=fieldKey;
		this.fieldName=fieldName;
		this.formater=df_formater;
	}
	public TableHeader(String fieldKey, String fieldName, int columnWidth)
	{
		this.fieldKey=fieldKey;
		this.fieldName=fieldName;
		this.columnWidth=columnWidth;
		this.formater=df_formater;
	}
	public TableHeader(String fieldKey, String fieldName, DataFormater df)
	{
		this.fieldKey=fieldKey;
		this.fieldName=fieldName;
		this.formater=df;
	}
	public TableHeader(String fieldKey, String fieldName, int columnWidth, DataFormater df)
	{
		this.fieldKey=fieldKey;
		this.fieldName=fieldName;
		this.columnWidth=columnWidth;
		this.formater=df;
	}
	/**
	 * 添加一个子标头
	 * @param childField
	 * @return 当前标头
	 */
	public TableHeader addChildField(TableHeader childField)
	{
		if(this.childFiled==null)
			this.childFiled=new ArrayList<TableHeader>();
		this.childFiled.add(childField);
		childField.parentHeader=this;
		return this;
	}
	public TableHeader addChildField(String fieldName)
	{
		return addChildField(null,fieldName,defColumnWidth,null);
	}
	public TableHeader addChildField(String fieldKey, String fieldName)
	{
		return addChildField(fieldKey,fieldName,defColumnWidth,new DefaultFormater());
	}
	public TableHeader addChildField(String fieldKey, String fieldName,DataFormater df)
	{
		return addChildField(fieldKey,fieldName,defColumnWidth,df);
	}
	public TableHeader addChildField(String fieldKey, String fieldName, int columnWidth)
	{
		return addChildField(fieldKey,fieldName,columnWidth,new DefaultFormater());
	}
	public TableHeader addChildField(String fieldKey, String fieldName, int columnWidth, DataFormater df) {
		TableHeader tf = new TableHeader(fieldKey, fieldName, columnWidth,df);
		tf.parentHeader=this;
		addChildField(tf);
		return tf;
	}
	/**
	 * 添加一组字标头
	 * @param childFields
	 * @return 当前标头
	 */
	public TableHeader addChildField(List<TableHeader> childFields)
	{
		if(this.childFiled==null)
			this.childFiled=new ArrayList<TableHeader>();
		this.childFiled.addAll(childFields);
		for(TableHeader th:childFields)
			th.parentHeader=this;
		return this;
	}
	public int getLevelIndex(){
		if(parentHeader==null)
			return 0;
		else
			return parentHeader.getLevelIndex()+1;
	}
	/**
	 * 通过递归计算指定的表头下最底层的标头级数,并将此标头作为顶层标头依次向下初始化个子标头的行索引
	 * @param currentIndex 当前标头的索引号
	 * @return 最低层的标头级数
	 */
	protected int countRowIndex(int currentIndex)
	{
		int maxRow=currentIndex;
		if(childFiled==null)
			return currentIndex;
		++currentIndex;
		for(TableHeader tf:childFiled)
		{
			int num=tf.countRowIndex(currentIndex);
			if(maxRow<num)
				maxRow=num;
		}
		return maxRow;
	}
	/**
	 * 获取当前标头下含有的最底层标头的数量
	 * @return
	 */
	public int getDataFieldNum()
	{
		int num=0;
		if(getChildFiled()==null)
			return 1;
		for(TableHeader t:getChildFiled())
		{
			num=num+t.getDataFieldNum();
		}
		return num;	
	}
	/**
	 * 获取指定对象中的某个属性或Map中的值
	 * @param obj 要获取的对象
	 * @return
	 */
	public Object getDataValue(Object obj)
	{
		try
		{
			//针对map类型的对象获取值
			if(obj instanceof Map)
			{
				@SuppressWarnings("rawtypes")
				Map map=(Map)obj;
				return formater.formater(map.get(fieldKey));
			}
			//针对其他对象的获取方法,获取fieldKey对应的对象属性
			else
			{
				Class<? extends Object> c=obj.getClass();
				for(Field f:c.getDeclaredFields())
				{
					if(f.getName().equalsIgnoreCase(fieldKey))
					{
						f.setAccessible(true);
						return formater.formater(f.get(obj));
					}
				}
			}
		}catch (Exception e) {
			return "";
		}
		return "";
	}
	public List<TableHeader> getChildFiled()
	{
		return childFiled;
	}

	public DataFormater getFormater() {
		return formater;
	}
	public void setFormater(DataFormater formater) {
		if(formater!=null)
			this.formater = formater;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public String getFieldKey() {
		return fieldKey;
	}

	public void setFieldKey(String fieldKey) {
		this.fieldKey = fieldKey;
	}

	public int getColumnWidth() {
		return columnWidth;
	}

	public void setColumnWidth(int columnWidth) {
		this.columnWidth = columnWidth;
	}

	public TableHeader getParentHeader() {
		return parentHeader;
	}
}
